{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0a3fbc21",
   "metadata": {},
   "outputs": [],
   "source": [
    "dct = {'学号': '001', '姓名': 'zhangsan', '年龄': 19}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "abc47f3a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhangsan'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dct['姓名']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "31abf72a",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'性别'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32mD:\\Temp\\ipykernel_6112\\2835178322.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdct\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'性别'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: '性别'"
     ]
    }
   ],
   "source": [
    "dct['性别']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "548c0b7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "dct['性别']='男'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "188a8215",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'学号': '001', '姓名': 'zhangsan', '年龄': 19, '性别': '男'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dct"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6d476797",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__class__',\n",
       " '__class_getitem__',\n",
       " '__contains__',\n",
       " '__delattr__',\n",
       " '__delitem__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__getitem__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__ior__',\n",
       " '__iter__',\n",
       " '__le__',\n",
       " '__len__',\n",
       " '__lt__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__or__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__reversed__',\n",
       " '__ror__',\n",
       " '__setattr__',\n",
       " '__setitem__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " 'clear',\n",
       " 'copy',\n",
       " 'fromkeys',\n",
       " 'get',\n",
       " 'items',\n",
       " 'keys',\n",
       " 'pop',\n",
       " 'popitem',\n",
       " 'setdefault',\n",
       " 'update',\n",
       " 'values']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(dct)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "71228075",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'学号': '001', '姓名': 'zhangsan', '年龄': 19, '性别': '男'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dct"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a9e02cc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "dct2=dct.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1a275cf4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'学号': '001', '姓名': 'zhangsan', '年龄': 19, '性别': '男'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dct2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "588235f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dct==dct2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "437cb591",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dct2.get('年龄')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3a79c25c",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'年龄'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32mD:\\Temp\\ipykernel_6112\\319295790.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdct\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'年龄'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: '年龄'"
     ]
    }
   ],
   "source": [
    "x=dct.pop('年龄')      #删除元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1ffcfa2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'学号': '001', '姓名': 'zhangsan', '年龄': 19, '性别': '男'}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dct2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7e3f4775",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'学号': '001', '姓名': 'zhangsan', '性别': '男'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dct"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "53b817c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['学号', '姓名', '性别'])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dct.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "e09ddbce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学号\n",
      "姓名\n",
      "年龄\n",
      "性别\n"
     ]
    }
   ],
   "source": [
    "for k in dct2.keys():\n",
    "    print(k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "e13e1275",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['001', 'zhangsan', 19, '男'])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dct2.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "9259fee4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('学号', '001'), ('姓名', 'zhangsan'), ('性别', '男')])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dct.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c9021888",
   "metadata": {},
   "outputs": [],
   "source": [
    "s= 'Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming.'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "28495f1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'p': 7, 'y': 2, 't': 12, 'h': 5, 'o': 9, 'n': 9, ' ': 23, 'i': 10, 's': 6, 'a': 14, 'e': 16, 'l': 6, 'r': 10, ',': 1, 'w': 1, 'f': 5, 'u': 5, 'g': 7, 'm': 5, '.': 2, 'c': 5, '-': 2, 'v': 2, 'd': 3, 'b': 2, 'j': 1}\n"
     ]
    }
   ],
   "source": [
    "dct={}        # 统计结果类似 {'p':21, 'a':5,  'x':2}\n",
    "for c in s.lower():\n",
    "    dct[c]=dct.get(c,0) +1\n",
    "print(dct)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "2ea87b9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst=sorted(dct.items(),key=lambda x:x[1],reverse= True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "f55b0a79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(' ', 23),\n",
       " ('e', 16),\n",
       " ('a', 14),\n",
       " ('t', 12),\n",
       " ('i', 10),\n",
       " ('r', 10),\n",
       " ('o', 9),\n",
       " ('n', 9),\n",
       " ('p', 7),\n",
       " ('g', 7),\n",
       " ('s', 6),\n",
       " ('l', 6),\n",
       " ('h', 5),\n",
       " ('f', 5),\n",
       " ('u', 5),\n",
       " ('m', 5),\n",
       " ('c', 5),\n",
       " ('d', 3),\n",
       " ('y', 2),\n",
       " ('.', 2),\n",
       " ('-', 2),\n",
       " ('v', 2),\n",
       " ('b', 2),\n",
       " (',', 1),\n",
       " ('w', 1),\n",
       " ('j', 1)]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
